package com.meng.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.meng.netty.constant.PointOfSaleConstant;
import com.meng.model.upload.UploadBlackListRecord;
import com.meng.model.upload.UploadConsumeRecord;
import io.netty.buffer.ByteBuf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.List;


/**
 * pos 请求 model
 *
 * @author MENG
 * @version 2018/3/22
 * @see
 */
public class PointOfSale implements Serializable
{
    @JsonIgnore
    protected Logger logger = LoggerFactory.getLogger(PointOfSale.class);

    @JsonIgnore
    protected byte[] two = new byte[2];

    @JsonIgnore
    protected byte[] four = new byte[4];

    @JsonIgnore
    protected byte[] eight = new byte[8];

    @JsonIgnore
    protected byte[] twelve = new byte[12];

    @JsonIgnore
    protected byte[] thirteen = new byte[13];

    @JsonIgnore
    protected byte[] fourteen = new byte[14];

    @JsonIgnore
    protected byte[] sixteen = new byte[16];

    @JsonIgnore
    protected byte[] eighteen = new byte[18];

    @JsonIgnore
    protected byte[] twenty = new byte[20];

    @JsonIgnore
    protected String utf8Encoding = "UTF-8";//字符编码

    /**
     * 请求头
     */
    @JsonIgnore
    protected String head;

    /**
     * 消息长度
     */
    @JsonIgnore
    protected short length;

    /**
     * 1个字节
     *
     * 有符号（'-'，'+'） 数字范围 -127 ～ 128
     *
     * 消息类型
     */
    @JsonIgnore
    protected byte type;

    /**
     * 结束符
     */
    @JsonIgnore
    protected String end;

    /**
     * 消息主体  List
     */
    @JsonIgnore
    protected List<PointOfSale> pointOfSales;

    public String getHead()
    {
        return head;
    }

    public void setHead(String head)
    {
        this.head = head;
    }

    public short getLength()
    {
        return length;
    }

    public void setLength(short length)
    {
        this.length = length;
    }

    public byte getType()
    {
        return type;
    }

    public void setType(byte type)
    {
        this.type = type;
    }

    public String getEnd()
    {
        return end;
    }

    public void setEnd(String end)
    {
        this.end = end;
    }




    /**
     * 根据不同的消息类型解码
     *
     * @param byteBuf
     * @return
     */
    public static PointOfSale getPointOfSaleByType(ByteBuf byteBuf,String head) throws UnsupportedEncodingException
    {
        //类型
        byte type = byteBuf.readByte();

        //长度
        short length = byteBuf.readShortLE();

        PointOfSale pointOfSale = null;

        if(PointOfSaleConstant.HEART_BEAT == type)//心跳
        {
            pointOfSale = new PointOfSale();
        }
        else if(PointOfSaleConstant.CONSUME_RECORD == type)//消费记录
        {
            pointOfSale = new UploadConsumeRecord();

            pointOfSale.pointOfSales = pointOfSale.decoderBody(byteBuf,length);

        }
        else if(PointOfSaleConstant.BLACK_LIST_RECORD == type)//黑名单
        {
            pointOfSale = new UploadBlackListRecord();

            pointOfSale.pointOfSales = pointOfSale.decoderBody(byteBuf,length);
        }
        else
        {
            return null;//非法消息类型
        }

        pointOfSale.setHead(head);

        pointOfSale.setType(type);

        pointOfSale.setLength(length);

        byte[] end = new byte[3];

        byteBuf.readBytes(end);

        pointOfSale.setEnd(new String(end));

        return pointOfSale;
    }


    /**
     *
     * 子类重写 主体解码
     *
     * @param byteBuf
     */
    public List<PointOfSale> decoderBody(ByteBuf byteBuf,short length) throws UnsupportedEncodingException
    {
        return null;
    }


    /**
     *
     * 子类重写 主体编码
     *
     * @param byteBuf
     */
    public void encoderBody(ByteBuf byteBuf)
    {

    }

    /**
     *
     * 将数据写入Redis
     *
     * @return
     */
    public void writeRedis()
    {

    }



    @Override
    public String toString()
    {
        return "PointOfSale{" +
               "head='" + head + '\'' +
               ", length=" + length +
               ", type=" + type +
               ", end='" + end + '\'' +
               '}';
    }
}
